﻿using MQTTnet.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Common.Entitys.Messages
{
    /// <summary>
    /// 进程间通信的日志消息(非阻塞)
    /// </summary>
    //[Obsolete("建议使用MqttMsg,但依然保持兼容")]
    [Serializable]
    public class IPCMsg/* : ISerializable*/
    {
        public bool IsSelfID = true;
        /// <summary>
        /// 所发送的对象
        /// </summary>
        public object Obj;
        /// <summary>
        /// 对象的ID号
        /// </summary>
        public Guid TypeGuid;
        /// <summary>
        /// 对象的程序集名称
        /// </summary>
        public string AssemblyQualifiedName { get; set; }

        /// <summary>
        /// 发送次数(最多一次 最少一次 精准一次)
        /// </summary>
        [NonSerialized]
        public MqttQualityOfServiceLevel MqttQualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce;

        /// <summary>
        /// 客户端ID
        /// </summary>
        public string ClientId;

        public string Contract;

        public IPCMsg()
        {

        }
        /// <summary>
        /// 传递一个对象
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="Exception"></exception>
        public IPCMsg(object obj)
        {
            if (obj == null)
            {
                return;
            }

            if (!obj.GetType().IsSerializable)
            {
                throw new Exception($"{obj.GetType().Name} 未标记为可序列化");
            }
            Obj = obj;
            TypeGuid = obj.GetType().GUID;
            AssemblyQualifiedName = obj.GetType().AssemblyQualifiedName;
        }

        //public IPCMsg(SerializationInfo info, StreamingContext context)
        //{
        //    IsSelfID = info.GetBoolean(nameof(IsSelfID));
        //    Obj = info.GetValue(nameof(Obj), typeof(object));
        //    ClientId = info.GetString(nameof(ClientId));
        //    if (!IsSelfID)
        //    {
        //        TypeGuid = (Guid)info.GetValue(nameof(TypeGuid), typeof(Guid));
        //    }
        //    else
        //    {
        //        TypeGuid = Obj.GetType().GUID;
        //    }
        //}

        //public void GetObjectData(SerializationInfo info, StreamingContext context)
        //{
        //    info.AddValue(nameof(IsSelfID), IsSelfID);
        //    info.AddValue(nameof(Obj), Obj);
        //    info.AddValue(nameof(ClientId),ClientId);
        //    info.AddValue(nameof(FullName), FullName);
        //    if (!IsSelfID)
        //    {
        //        info.AddValue(nameof(TypeGuid), TypeGuid);
        //    }
        //}

        public static implicit operator IPCMsg(ActionMsg actionMsg)
        {
            return new IPCMsg(actionMsg);
        }


    }

    /// <summary>
    /// 应答阻塞式消息
    /// </summary>

    [Serializable]
    public class IPCBlockMsg
    {
        public bool IsSelfID = true;

        public object Obj;

        public Guid TypeGuid;

        public string AssemblyQualifiedName { get; set; }

        /// <summary>
        /// 发送次数(最多一次 最少一次 精准一次)
        /// </summary>
        [NonSerialized]
        public MqttQualityOfServiceLevel MqttQualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce;

        /// <summary>
        /// 客户端ID
        /// </summary>
        public string ClientId;

        public IPCBlockMsg()
        {

        }

        public IPCBlockMsg(object obj)
        {
            if (obj == null)
            {
                return;
            }

            if (!obj.GetType().IsSerializable)
            {
                throw new Exception($"{obj.GetType().Name} 未标记为可序列化");
            }
            Obj = obj;
            TypeGuid = obj.GetType().GUID;
            AssemblyQualifiedName = obj.GetType().AssemblyQualifiedName;
        }

        public static implicit operator IPCBlockMsg(ActionMsg actionMsg)
        {
            return new IPCBlockMsg(actionMsg);
        }
    }

    /// <summary>
    /// 应答结果
    /// </summary>
    [Serializable]
    public class IPCBlockResult
    {
        public bool Result { get; set; }

        public string Error { get; set; }
    }

    ///// <summary>
    ///// 进程间通信的日志消息
    ///// </summary>
    //[Obsolete("建议使用MqttMsg,但依然保持兼容")]
    //[Serializable]
    //public class IPCMsg<T> : ISerializable
    //{
    //    public bool IsSelfID = true;

    //    public T Obj;

    //    public Guid TypeGuid;

    //    public IPCMsg(T t)
    //    {
    //        Obj = t;
    //    }

    //    public IPCMsg(SerializationInfo info, StreamingContext context)
    //    {
    //        IsSelfID = info.GetBoolean(nameof(IsSelfID));
    //        Obj = (T)info.GetValue(nameof(Obj), typeof(object));
    //        if (!IsSelfID)
    //        {
    //            TypeGuid = (Guid)info.GetValue(nameof(TypeGuid), typeof(Guid));
    //        }
    //        else
    //        {
    //            TypeGuid = Obj.GetType().GUID;
    //        }
    //    }

    //    public void GetObjectData(SerializationInfo info, StreamingContext context)
    //    {
    //        info.AddValue(nameof(IsSelfID), IsSelfID);
    //        info.AddValue(nameof(Obj), Obj);
    //        if (!IsSelfID)
    //        {
    //            info.AddValue(nameof(TypeGuid), TypeGuid);
    //        }
    //    }
    //}
}
